import {
	storage
} from '@/utils/cache'
import Vue from 'vue'

// let InterstitialAd = qq.createInterstitialAd({
// 	adUnitId: 'e7e0196696e368c515a8cee5da6e7479'
// });

const types = {
	InterstitialAd: 'InterstitialAd',
	pageName: 'pageName', //页面名称
	time: 'time', // 上次播放时间/小程序启动时间
	showNum: 'showNum', // 显示的次数
	showState: 'showState', // 显示状态
	isOn: 'isOn', //是否已开始监控
	timer: 'timer', //定时器
	successFb: 'successFb', //成功观看回调函数
	failFb: 'failFb', //观看失败回调函数
}

const state = {
	[types.InterstitialAd]: null,
	[types.pageName]: null,
	[types.showNum]: 0,
	[types.time]: null,
	[types.showState]: false,
	[types.isOn]: false,
	[types.timer]: null,
	[types.successFb]: null,
	[types.failFb]: null,
}

const getters = {
	_InterstitialAd(state) {
		return state[types.InterstitialAd]
	},
	_pageName(state) {
		return state[types.pageName]
	},
}

const actions = {
	init: async ({
		commit,
		dispatch,
		state
	}, {
		InterstitialAd = null,
		pageName = null
	}) => {
		commit(types.InterstitialAd, InterstitialAd)
		commit(types.pageName, pageName)
		if (!state[types.isOn]) {
			commit(types.isOn, true)
			state[types.InterstitialAd].onError(res => {
				dispatch('setTimer')
				console.log('InterstitialAd onError', res);
			});

			state[types.InterstitialAd].onLoad((res) => {
				commit(types.showState, false)
				console.log('InterstitialAd onLoad', res);
			})

			state[types.InterstitialAd].onClose((res) => {
				const num = state[types.showNum] + 1
				commit(types.showNum, num)
				commit(types.showState, false)
				commit(types.time, new Date().getTime())
				console.log('close event emit')
			})
			console.info('init', state)
			dispatch('setTimer')
		}
		return state[types.InterstitialAd]
	},
	show: async ({
		commit,
		dispatch,
		state
	}, {
		success,
		fail
	} = {}) => {
		console.info('show', state)
		let isShow = false
		if (state[types.isOn] && !state[types.showState]) {
			console.info('isOn', state)
			// uni.showLoading({
			// 	title: '加载中',
			// 	mask: true
			// });
			commit(types.successFb, success)
			commit(types.failFb, fail)
			await state[types.InterstitialAd]
				.load()
				.then(async () => {
					console.log('插屏广告加载成功');
					await state[types.InterstitialAd]
						.show()
						.then(() => {
							commit(types.showState, true)
							isShow = true
							uni.hideLoading();
							console.log('插屏广告 广告显示成功');
						})
						.catch(err => {
							console.log('插屏广告 广告显示失败', err);
							commit(types.pageName, null)
						});
				})
				.catch(err => {
					console.log('插屏广告加载失败');
				});
		}
		return isShow
	},
	destroy: async ({
		commit,
		dispatch,
		state
	}) => {
		if (state[types.InterstitialAd]) {
			if (state[types.timer]) {
				clearTimeout(state[types.timer])
			}
			state[types.InterstitialAd].destroy()
			commit(types.InterstitialAd, null)
			commit(types.isOn, false)
			commit(types.showState, false)
			commit(types.timer, null)
			commit(types.successFb, null)
			commit(types.failFb, null)
		}
	},
	setTimer: async ({
		state,
		commit,
		dispatch,
	}) => {
		//启动小程序(即第一次需要)半分钟才可以显示插屏,两次插屏时间必须有1分钟 
		let time = state[types.showNum] == 0 ? 30000 : 60000
		if (!state[types.time]) {
			commit(types.time, new Date().getTime())
		}
		const num = new Date().getTime() - state[types.time];
		console.log('types.time num', num)
		time = time - num
		time = time > 1000 ? time : 1000
		console.log('types.time time', time)
		if (!state[types.timer]) {
			const timer = setTimeout(() => {
				dispatch('show')
				clearTimeout(state[types.timer])
			}, time)
			commit(types.timer, timer)
		}
	},

}

const mutations = {
	[types.InterstitialAd]: (state, res) => {
		state[types.InterstitialAd] = res
	},
	[types.pageName]: (state, res) => {
		state[types.pageName] = res
	},
	[types.time]: (state, res) => {
		state[types.time] = res
	},
	[types.showNum]: (state, res) => {
		state[types.showNum] = res
	},
	[types.isOn]: (state, res) => {
		state[types.isOn] = res
	},
	[types.showState]: (state, res) => {
		state[types.showState] = res
	},
	[types.timer]: (state, res) => {
		state[types.timer] = res
	},
	[types.successFb]: (state, res) => {
		state[types.successFb] = res
	},
	[types.failFb]: (state, res) => {
		state[types.failFb] = res
	},
}

export default {
	namespaced: true,
	types,
	state,
	getters,
	actions,
	mutations
}
